﻿using MediatR;
using Product.Domain;
using Product.Domain.DTO;
using Product.Infrstructre.Impl;
using Product.Read.API.Applcation.Command;

namespace Product.Read.API.Applcation.CommandHandlers
{
    public class ProductSelectCommandHandler : IRequestHandler<ProductSelectCommand, ProductTypeDTO>
    {
        protected readonly IbaseProductRepository ibase;

        public ProductSelectCommandHandler(IbaseProductRepository ibase)
        {
            this.ibase = ibase;
        }

        /// <summary>
        /// 查询所有产品类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ProductTypeDTO> Handle(ProductSelectCommand request, CancellationToken cancellationToken)
        {
            var list = (await ibase.GetAllAsync()).Where(x => x.IsDel == false && x.Office_Id == request.Office_Id);
            var size = (request.pageindex - 1) * request.pagesize;
            if (request.ProductTypeCode != null)
            {
                list = list.Where(x => x.ProductTypeCode.Contains(request.ProductTypeCode)).ToList();
            }
            if (request.ProductTypeName != null)
            {
                list = list.Where(x => x.ProductTypeName.Contains(request.ProductTypeName)).ToList();
            }
            var page = new ProductTypeDTO();
            page.pagecount = list.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / request.pagesize);
            page.list = list.ToList();
            return page;
        }
    }
}
